/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.air.updateservice
{
	import air.update.ApplicationUpdaterUI;
	import air.update.events.UpdateEvent;
	
	import com.screenworker.compactmvc.extensions.air.interfaces.IUpdateService;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import flash.desktop.NativeApplication;
	import flash.events.ErrorEvent;
	
	import mx.controls.Alert;

	public class UpdateService extends Model implements IModel, IUpdateService
	{
		private var appUpdater:ApplicationUpdaterUI;
		 
		public function UpdateService(name:String, controller:Controller)
		{
			super(name, controller);
			appUpdater = new ApplicationUpdaterUI()
		} 
		
		override public function removeHandler():void
		{
			appUpdater.cancelUpdate();
			appUpdater = null;
		}
		
		/**
		 * 
		 * @param updateXMLUrl url to the update.xml
		 * @param isCheckForUpdateVisible
		 * 
		 * update.xml:
		 * &lt; ?xml version="1.0" encoding="utf-8"?&gt; 
		 * &lt; update xmlns="http://ns.adobe.com/air/framework/update/description/1.0"&gt; 
		 * &lt; version&gt; 0.1.0&lt; /version&gt; 
		 * &lt; url&gt; http://myDomain.de/MyAirApplication.air&lt; /url&gt; 
		 * &lt; description&gt; &lt; ![CDATA[
		 * 
		 * ]]&gt; &lt; /description&gt; 
		 * &lt; /update&gt; 
		 */		
		protected function checkForUpdate(updateXMLUrl:String, isCheckForUpdateVisible:Boolean=false):void 
        {
            // Server-side XML file describing update
            appUpdater.updateURL = updateXMLUrl;
            // We won't ask permission to check for an update
            appUpdater.isCheckForUpdateVisible = isCheckForUpdateVisible; 
            // Once initialized, run onUpdate
            appUpdater.addEventListener(UpdateEvent.INITIALIZED, updateHandler, false, 0, true); 
            // If something goes wrong, run onError
            appUpdater.addEventListener(ErrorEvent.ERROR, errorHandler, false, 0, true); 
            // Initialize the update framework
            appUpdater.initialize(); 
        }
        
        /**
         * update Handler 
         * @param event
         * 
         */        
        private function updateHandler(event:UpdateEvent):void 
        {
        	// Go check for an update now
            appUpdater.checkNow(); 
        }
		
        /**
         * error handler 
         * @param event
         * 
         */		
        public function errorHandler(event:ErrorEvent):void 
        {
           //overriden by subclass
        }
        
         /**
         * returns the applicationVersion 
         * @return 
         * 
         */        
        protected function get applicationVersion():String 
        {
            var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
            var ns:Namespace = appXML.namespace();
            return appXML.ns::version;
        }	   
	}
}
